﻿#region 头注释
/******************************************************************************
文件名称    :   Optics.cs
创建日期    :   2011/5/11 22:14:19
功能描述    :   Optics实体类
修改日志    :   
    2011-05-11 22:14  马利
*******************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
using Dare.Utilities.Data;

namespace Stone.Components.Entities
{
    /// <summary>
    /// Optics实体类
    /// </summary>
    public class Optics : EntityBase<Optics, long>
    {
        #region 构造函数

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public Optics()
            : base()
        {
            base.xmlSerializer = new DefaultXmlSerializer(this);
            base.jsonSerializer = new DefaultJsonSerializer(this);
        }

        public override void Reset()
        {
            typeBUltrasonicFile = null;
            typeBUltrasonicFileId = null;
            cTFile = null;
            cTFileId = null;
            cTvalue = null;
            cTUFile = null;
            cTUFileId = null;
            kUBIVPFile = null;
            kUBIVPFileId = null;
            eCTFile = null;
            eCTFileId = null;
            gFRvalue = null;
            angle = null;
            comment = null;
            timeStamp = DateTime.MinValue;
        }

        public override Optics CreateNew()
        {
            return new Optics();
        }

        #endregion

        #region 属性

        /// <summary>
        /// 获取实体类型
        /// </summary>
        public override EntityType EntityType
        {
            get { return EntityType.Optics; }
        }

        /// <summary>
        /// 获取或设置B超图像文件id
        /// </summary>
        public MediaFile TypeBUltrasonicFile
        {
            get { return typeBUltrasonicFile; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != typeBUltrasonicFileId) entityState = EntityState.Changed;
                typeBUltrasonicFile = value;
            }
        }
        protected MediaFile typeBUltrasonicFile;

        /// <summary>
        /// 获取或设置B超图像文件id
        /// </summary>
        public long? TypeBUltrasonicFileId
        {
            get { return typeBUltrasonicFile == null ? typeBUltrasonicFileId : typeBUltrasonicFile.Id; }
            set { typeBUltrasonicFileId = value; }
        }
        protected long? typeBUltrasonicFileId;

        /// <summary>
        /// 获取或设置CT图片文件id
        /// </summary>
        public MediaFile CTFile
        {
            get { return cTFile; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != cTFileId) entityState = EntityState.Changed;
                cTFile = value;
            }
        }
        protected MediaFile cTFile;

        /// <summary>
        /// 获取或设置CT图片文件id
        /// </summary>
        public long? CTFileId
        {
            get { return cTFile == null ? cTFileId : cTFile.Id; }
            set { cTFileId = value; }
        }
        protected long? cTFileId;

        /// <summary>
        /// 获取或设置结合CT值
        /// </summary>
        public int? CTvalue
        {
            get { return cTvalue; }
            set
            {
                if (entityState == EntityState.Saved && cTvalue != value) entityState = EntityState.Changed;
                cTvalue = value;
            }
        }
        protected int? cTvalue;

        /// <summary>
        /// 获取或设置CTU图像文件id
        /// </summary>
        public MediaFile CTUFile
        {
            get { return cTUFile; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != cTUFileId) entityState = EntityState.Changed;
                cTUFile = value;
            }
        }
        protected MediaFile cTUFile;

        /// <summary>
        /// 获取或设置CTU图像文件id
        /// </summary>
        public long? CTUFileId
        {
            get { return cTUFile == null ? cTUFileId : cTUFile.Id; }
            set { cTUFileId = value; }
        }
        protected long? cTUFileId;

        /// <summary>
        /// 获取或设置KUB_IVP图像文件id
        /// </summary>
        public MediaFile KUBIVPFile
        {
            get { return kUBIVPFile; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != kUBIVPFileId) entityState = EntityState.Changed;
                kUBIVPFile = value;
            }
        }
        protected MediaFile kUBIVPFile;

        /// <summary>
        /// 获取或设置KUB_IVP图像文件id
        /// </summary>
        public long? KUBIVPFileId
        {
            get { return kUBIVPFile == null ? kUBIVPFileId : kUBIVPFile.Id; }
            set { kUBIVPFileId = value; }
        }
        protected long? kUBIVPFileId;

        /// <summary>
        /// 获取或设置ECT图像文件id
        /// </summary>
        public MediaFile ECTFile
        {
            get { return eCTFile; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != eCTFileId) entityState = EntityState.Changed;
                eCTFile = value;
            }
        }
        protected MediaFile eCTFile;

        /// <summary>
        /// 获取或设置ECT图像文件id
        /// </summary>
        public long? ECTFileId
        {
            get { return eCTFile == null ? eCTFileId : eCTFile.Id; }
            set { eCTFileId = value; }
        }
        protected long? eCTFileId;

        /// <summary>
        /// 获取或设置肾小球滤过率(GFR)
        /// </summary>
        public decimal? GFRvalue
        {
            get { return gFRvalue; }
            set
            {
                if (entityState == EntityState.Saved && gFRvalue != value) entityState = EntityState.Changed;
                gFRvalue = value;
            }
        }
        protected decimal? gFRvalue;

        /// <summary>
        /// 获取或设置下盏和剩余的夹角
        /// </summary>
        public decimal? Angle
        {
            get { return angle; }
            set
            {
                if (entityState == EntityState.Saved && angle != value) entityState = EntityState.Changed;
                angle = value;
            }
        }
        protected decimal? angle;

        /// <summary>
        /// 获取或设置备注
        /// </summary>
        public string Comment
        {
            get { return comment; }
            set
            {
                if (entityState == EntityState.Saved && comment != value) entityState = EntityState.Changed;
                comment = value;
            }
        }
        protected string comment;

        /// <summary>
        /// 获取或设置更新时间戳
        /// </summary>
        public DateTime TimeStamp
        {
            get { return timeStamp; }
            set
            {
                if (entityState == EntityState.Saved && timeStamp != value) entityState = EntityState.Changed;
                timeStamp = value;
            }
        }
        protected DateTime timeStamp;

        #endregion

        #region 方法

        #endregion

        #region 实体复制相关方法

        /// <summary>
        /// 复制当前实体属性到指定实体上
        /// </summary>
        /// <param name="entity">要把属性复制到的实体</param>
        /// <returns>返回传入的实体</returns>
        public override Optics CopyTo(Optics entity)
        {
            if (base.CopyTo(entity) == null) return null;

            entity.typeBUltrasonicFile = this.typeBUltrasonicFile;
            entity.typeBUltrasonicFileId = this.typeBUltrasonicFileId;
            entity.cTFile = this.cTFile;
            entity.cTFileId = this.cTFileId;
            entity.cTvalue = this.cTvalue;
            entity.cTUFile = this.cTUFile;
            entity.cTUFileId = this.cTUFileId;
            entity.kUBIVPFile = this.kUBIVPFile;
            entity.kUBIVPFileId = this.kUBIVPFileId;
            entity.eCTFile = this.eCTFile;
            entity.eCTFileId = this.eCTFileId;
            entity.gFRvalue = this.gFRvalue;
            entity.angle = this.angle;
            entity.comment = this.comment;
            entity.timeStamp = this.timeStamp;

            return entity;
        }

        #endregion

        #region 默认XML序列器类

        public class DefaultXmlSerializer : XmlSerializerBase, IEntityXmlSerializer<Optics>
        {
            Optics entity;
            public DefaultXmlSerializer(Optics entity)
                : base(entity.GetType().Name)
            {
                this.entity = entity;
            }

            public Optics Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            protected override void BeforeReadXml()
            {
                entity.Reset();
            }

            /// <summary>
            /// 读取XML属性
            /// </summary>
            /// <param name="name">属性名</param>
            /// <param name="value">属性值</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlAttribute(string name, string value, XmlReader reader)
            {
                switch (name)
                {
                    case "id":
                        entity.id = XmlConvert.ToInt64(value);
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(long.Parse(value));
                        break;
                    case "entityState":
                        entity.entityState = (EntityState)Enum.Parse(typeof(EntityState), value, true);
                        break;
                }
            }

            /// <summary>
            /// 读取XML子节点
            /// </summary>
            /// <param name="name">节点名</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlChildElement(string name, XmlReader reader)
            {
                switch (name)
                {
                    case "TypeBUltrasonicFileId":
                        entity.typeBUltrasonicFileId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "CTFileId":
                        entity.cTFileId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "CTvalue":
                        entity.cTvalue = XmlConvert.ToInt32(reader.ReadElementString());
                        break;
                    case "CTUFileId":
                        entity.cTUFileId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "KUBIVPFileId":
                        entity.kUBIVPFileId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "ECTFileId":
                        entity.eCTFileId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "GFRvalue":
                        entity.gFRvalue = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Angle":
                        entity.angle = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Comment":
                        entity.comment = reader.ReadElementString();
                        break;
                }
            }

            /// <summary>
            /// 写入XML属性
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlAttributes(XmlWriter writer)
            {
                writer.WriteAttributeString("id", XmlConvert.ToString(entity.Id));
                writer.WriteAttributeString("timeStamp", entity.TimeStamp.Ticks.ToString());
                writer.WriteAttributeString("entityState", entity.EntityState.ToString());
            }

            /// <summary>
            /// 写入XML子节点
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlChildElements(XmlWriter writer)
            {
                if (entity.TypeBUltrasonicFileId != null) writer.WriteElementString("TypeBUltrasonicFileId", XmlConvert.ToString(entity.TypeBUltrasonicFileId.Value));
                if (entity.CTFileId != null) writer.WriteElementString("CTFileId", XmlConvert.ToString(entity.CTFileId.Value));
                if (entity.CTvalue != null) writer.WriteElementString("CTvalue", XmlConvert.ToString(entity.CTvalue.Value));
                if (entity.CTUFileId != null) writer.WriteElementString("CTUFileId", XmlConvert.ToString(entity.CTUFileId.Value));
                if (entity.KUBIVPFileId != null) writer.WriteElementString("KUBIVPFileId", XmlConvert.ToString(entity.KUBIVPFileId.Value));
                if (entity.ECTFileId != null) writer.WriteElementString("ECTFileId", XmlConvert.ToString(entity.ECTFileId.Value));
                if (entity.GFRvalue != null) writer.WriteElementString("GFRvalue", XmlConvert.ToString(entity.GFRvalue.Value));
                if (entity.Angle != null) writer.WriteElementString("Angle", XmlConvert.ToString(entity.Angle.Value));
                if (entity.Comment != null) writer.WriteElementString("Comment", entity.Comment);
            }
        }

        #endregion

        #region 默认Json序列化器类

        public class DefaultJsonSerializer : JsonSerializerBase, IEntityJsonSerializer<Optics>
        {
            Optics entity;
            public DefaultJsonSerializer(Optics entity)
                : base()
            {
                this.entity = entity;
            }

            public Optics Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            /// <summary>
            /// 读取JSON属性
            /// </summary>
            /// <param name="propName">属性名称</param>
            /// <param name="propValue">属性值</param>
            /// <param name="reader">JSON读取器</param>
            protected override void ReadJsonProperty(string propName, object propValue, JsonReader reader)
            {
                switch (propName)
                {
                    case "id":
                        entity.id = DbConvert.ToNullableInt64(propValue) ?? entity.id;
                        break;
                    case "typeBUltrasonicFileId":
                        entity.typeBUltrasonicFileId = DbConvert.ToNullableInt64(propValue) ?? entity.typeBUltrasonicFileId;
                        break;
                    case "cTFileId":
                        entity.cTFileId = DbConvert.ToNullableInt64(propValue) ?? entity.cTFileId;
                        break;
                    case "cTvalue":
                        entity.cTvalue = DbConvert.ToNullableInt32(propValue) ?? entity.cTvalue;
                        break;
                    case "cTUFileId":
                        entity.cTUFileId = DbConvert.ToNullableInt64(propValue) ?? entity.cTUFileId;
                        break;
                    case "kUBIVPFileId":
                        entity.kUBIVPFileId = DbConvert.ToNullableInt64(propValue) ?? entity.kUBIVPFileId;
                        break;
                    case "eCTFileId":
                        entity.eCTFileId = DbConvert.ToNullableInt64(propValue) ?? entity.eCTFileId;
                        break;
                    case "gFRvalue":
                        entity.gFRvalue = DbConvert.ToNullableDecimal(propValue) ?? entity.gFRvalue;
                        break;
                    case "angle":
                        entity.angle = DbConvert.ToNullableDecimal(propValue) ?? entity.angle;
                        break;
                    case "comment":
                        entity.comment = Convert.ToString(propValue) ?? entity.comment;
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(DbConvert.ToNullableInt64(propValue) ?? 0);
                        break;
                }
            }

            /// <summary>
            /// 写入JSON属性
            /// </summary>
            /// <param name="writer">JSON编写器</param>
            protected override void WriteJsonProperties(JsonWriter writer)
            {

                //Id
                writer.WritePropertyName("id");
                writer.WriteValue(entity.Id);

                //B超图像文件id
                writer.WritePropertyName("typeBUltrasonicFileId");
                if (entity.TypeBUltrasonicFileId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.TypeBUltrasonicFileId.Value);
                }

                //CT图片文件id
                writer.WritePropertyName("cTFileId");
                if (entity.CTFileId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.CTFileId.Value);
                }

                //结合CT值
                writer.WritePropertyName("cTvalue");
                if (entity.CTvalue == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.CTvalue.Value);
                }

                //CTU图像文件id
                writer.WritePropertyName("cTUFileId");
                if (entity.CTUFileId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.CTUFileId.Value);
                }

                //KUB_IVP图像文件id
                writer.WritePropertyName("kUBIVPFileId");
                if (entity.KUBIVPFileId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.KUBIVPFileId.Value);
                }

                //ECT图像文件id
                writer.WritePropertyName("eCTFileId");
                if (entity.ECTFileId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.ECTFileId.Value);
                }

                //肾小球滤过率(GFR)
                writer.WritePropertyName("gFRvalue");
                if (entity.GFRvalue == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.GFRvalue.Value);
                }

                //下盏和剩余的夹角
                writer.WritePropertyName("angle");
                if (entity.Angle == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Angle.Value);
                }

                //备注
                writer.WritePropertyName("comment");
                if (entity.Comment == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Comment);
                }

                //更新时间戳
                writer.WritePropertyName("timeStamp");
                writer.WriteValue(entity.TimeStamp.Ticks);
            }
        }


        #endregion
    }
}